package searcher.gui;

import java.io.File;
import java.io.FileFilter;

/**
 * Filters files by whether or not they are contained in a set of search
 * results. Parent directories of results are also included.
 * 
 * @version $Id: SearchResultsFileFilter.java,v 1.1 2006/04/05 14:14:42 abutler Exp $
 */
public class SearchResultsFileFilter implements FileFilter
{
	private final searcher.FileMatch[] theMatches;

	/**
	 * Creates a FileFilter on the given set of search results
	 * 
	 * @param matches The set of search results to filter on
	 */
	public SearchResultsFileFilter(searcher.FileMatch[] matches)
	{
		theMatches = matches;
	}

	public boolean accept(File pathname)
	{
		String mFile;
		for(int i = 0; i < theMatches.length; i++)
		{
			mFile = theMatches[i].getFile();
			if(mFile.startsWith(pathname.getPath()))
			{
				int len = pathname.getPath().length();
				if(mFile.length() == len || mFile.charAt(len) == '/'
					|| mFile.charAt(len) == '\\')
					return true;
			}
		}
		return false;
	}
}
